<?php
namespace Home\Controller;
use Think\Controller;
use Think\Db;

class RechargeController extends CommonController
{
    //充值中心页面
    public function RechargeCenter()
    {
        $agent_id = session('user_id');
        $avatar = M('agent_info')->where("agent_id = $agent_id")->find();
        $userinfo =M('agent')->where("agent_id = $agent_id")->find();
        $this->assign('userinfo',$userinfo);
        $this->assign('avatar',$avatar);
        $this->site_title = "充值中心";
        $this->display();

    }
    // 充值审核
    public function rechargeaudit()
    {
        $user_id = session('user_id');
        $where['audit_manager'] = array('eq', $user_id);
        $where['audit_status'] = array('gt', 0);
        $ret = D('AgentAccountView')->where($where)->order('record_time DESC')
            ->limit(0, 11)->select();
        $res = D('AgentAccountView')->where(array('audit_manager' => $user_id, 'audit_status' => 0))->order('record_time DESC')->limit(0, 11)->select();

        $this->site_title = "充值审核";
        $this->assign('res', $res);
        $this->assign('ret', $ret);
        $this->display();
    }

    public function moreaudit(){
        if(IS_AJAX){
            $pageSize = 11;
            $page = $_POST['page'] ? to_int(I("post.page")) : 1;
            $pageB = ($page - 1) * $pageSize;
            $audit_status = I("post.audit");
            $user_id = session('user_id');
            if($audit_status!=='n'){
                $where['audit_manager']=array('eq',$user_id);
                $where['audit_status']=array('gt',0);
                $res = D('AgentAccountView')->where($where)->limit($pageB, $pageSize)->order('record_time DESC')->select();
            }else{
                $res = D('AgentAccountView')->where(array('audit_manager'=>$user_id,'audit_status'=>0))->order('record_time DESC')->limit($pageB, $pageSize)->select();
            }
            if(empty($res)){
                echo 1;
            }else{
                $this->assign('res',$res);
                $this->assign('ast' ,$audit_status);
                $this->display();
            }
        }

    }
    // 充值审核
    public function rechargecheck()
    {
        // print_r($res);exit;
        if (IS_POST) {
            $id = I('post.account_id');
            $ret = D('AgentAccountView')->where(array('account_id'=>$id,"audit_status" => 0))->find();
            if(!$ret){
				$this->error("此充值您已审核",$_SERVER['HTTP_REFERER'] ,1);
            }
            $mobile = I('post.mobile');
            if($mobile!==$ret['mobile']){
                $this->error("参数有误",$_SERVER['HTTP_REFERER'] ,1);
            }
            $amount = I('post.amount');
            if($amount !== $ret['amount']){
                $this->error("参数有误",$_SERVER['HTTP_REFERER'] ,1);
            }
            if(empty($ret['parent_id'])){
                $savedate['audit_manager'] = get_director();
            }
            $status = I('post.status');
            $savedate['audit_status'] = I('post.status');
            $savedate['audit_time'] = NOW_TIME;
            $savedate['audit_remark'] = I('post.remark');

            if ($status == 1) {
                $rea = M('agent')->where(array('agent_id' => $ret['agent_id']))->find();
                $savedatea['money'] = $rea['money'] + $ret['amount'];
                M('agent')->where(array('agent_id' => $ret['agent_id']))->save($savedatea);
                $content = $ret['agent_name'] ."充值预存款：$amount 成功。";
            	wlm_write_log($ret['agent_id'] ,$content ,$amount ,1);
                M('agent_account')->where(array('account_id' => $id,"audit_status" => 0))->save($savedate);
            } else {
            	$content = $ret['agent_name'] ."充值预存款：$amount 未通过。";
            	wlm_write_log($ret['agent_id'] ,$content ,$amount ,3);
                M('agent_account')->where(array('account_id' => $id,"audit_status" => 0))->save($savedate);
            }

            // 充值之后向上级推送消息
            $openid = M('agent')->where(array('agent_id'=>$ret['agent_id']))->getField('openid');
            $name = $ret['agent_name'];
            $mes =M('model_message')->where(array('model_name'=>"充值消息通知"))->find();
            $date = date("Y-m-d H:i",time());
            $url = SITE_URL.U("Recharge/rechargepage");
            $type = "审核完成";
            $data = recharge_reminder($openid,$mes['model_id'],$url,$mes['content_begin'],$name,$type,$amount,$date,$mes['content_end']);
            Template_message($data);
            $this->success("审核成功",U("Recharge/rechargeaudit") ,1);
        }else{
        	$account_id = I('get.id');
        	$res = D('AgentAccountView')->where(array('account_id'=>$account_id))->find();
            $this->site_title = "审核";
        	$this->assign('res',$res);
        	$this->display();
        }
    }

    //充值页面
    public function RechargePage(){
        $agent_id = session('user_id');
        $avatar = M('agent_info')->where("agent_id = $agent_id")->find();
        $userinfo =M('agent')->where("agent_id = $agent_id")->find();
//        $blance = D('BrandMoney')->where(array('agent_id'=>$agent_id))->select();
        $this->site_title = "充值";
        $this->assign('userinfo',$userinfo);
//        $this->assign('blance',$blance);
        $this->assign('avatar',$avatar);
        $this->display();
    }

    //充值
    public function Recharge()
    {
        if (IS_POST) {
            $rechargemoney = I('post.rechargemoney');
            $rechargeps = I('post.rechargeps');
            //
            $agent_id = session('user_id');
            $time = NOW_TIME;
            $ip = $_SERVER["REMOTE_ADDR"];
            if ($rechargemoney != '') {
                if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != '') {
                    $up_rs = img_upload("file", "recharge");
                    if (isset($up_rs['error'])) {
                        $this->error($up_rs['error'], U('Recharge/recharge'), 1);
                    }
                    $enclosure = $up_rs['path'] . $up_rs['file'];
                }

                $my_parent_id = M("agent")->where("agent_id = %d" ,array($agent_id))->getField("parent_id");
                // 如果没有上级就是董事[没写
                if(!$my_parent_id || $my_parent_id == 0){
                    $result = M('agent_brand')->where(array('agent_id'=>$agent_id))->find();
                    $brand_id =$result['brand_id'];
                    $rank_id = M('agent_rank')->where(array('brand_id'=>$brand_id,'rank_grade'=>1))->getField('rank_id');
                    $id = M('agent_brand')->where(array('brand_id'=>$brand_id,'rank_id'=>$rank_id))->getField('agent_id');
                }else{
                    $id = $my_parent_id;
                }
                $ret = M("agent_account")->add(array("agent_id" => $agent_id,
                    "amount" => $rechargemoney,
                    "record_time" => $time,
                    "enclosure" => $enclosure,
                    "record_ip" => $ip,
                    "pay_type" => 0,
                    "record_remark" => $rechargeps,
                    "audit_manager" => $id
                ));

                // // 充值之后向上级推送消息
                // $rel = M('agent')->where(array('agent_id'=>session('user_id')))->getField('parent_id');
                // if(empty($rel)){
                //     // 没有上级就是董事
                //     $parent_id = get_director();
                // }else{
                //     $parent_id = $rel;
                // }
                // $openid = M('agent')->where(array('agent_id'=>$parent_id))->getField('openid');
                // $name = M('agent')->where(array('agent_id'=>$parent_id))->getField('agent_name');
                // $mes =M('model_message')->where(array('model_name'=>"充值消息通知"))->find();
                // $url = SITE_URL.U("Recharge/rechargeaudit");
                // $date = date("Y-m-d H:i",time());
                // $type = "上级推送";
                // $data = recharge_reminder($openid,$mes['model_id'],$url,$mes['content_begin'],$name,$type,$rechargemoney,$date,$mes['content_end']);
                // Template_message($data);
                $send_sms = M("config")->where("conf_key = 'order_send_sms'")->getField("conf_value");
                if($send_sms == 1){
                    $mobile = M("agent")->where("agent_id = %d" ,array($id))->getField("mobile");
                    $code = array("user" => GetAgentNameById($agent_id), "amt" => $rechargemoney, "time" => date("Y-m-d"));
                    send_sms($mobile, $code ,2);
                }
                $this->ajaxReturn(1);
            }else{
                $this->error("充值数额存在问题",$_SERVER['HTTP_REFERER'] ,1);
            }

            // 重复提交申请
            if (!$ret) {
                // 跳转到充值记录页面
                $this->error("提交失败", U('Recharge/rechargepage'), 1);
            } else {
                $this->success("提交成功", U('Recharge/rechargecenter'), 1);
            }
        }
    }
    //充值记录
    public function rechargerecord()
    {
        $agent_id = session('user_id');
        $where = array('agent_id' => $agent_id);
//        搜索
        if (isset($_REQUEST['rechargeinfo'])) {
            $rechargeinfo = trim(I('rechargeinfo'));
            if ($rechargeinfo != '') {
                $cWhere['amount'] = array("eq", $rechargeinfo);
                $cWhere['_logic'] = 'or';
                $where['_complex'] = $cWhere;
            }
            $res = M('agent_account')->where($where)->order('record_time DESC')->select();
        }else{
            $res = M('agent_account')->where($where)->order('record_time DESC')->limit(0,10)->select();
        }
            $this->site_title = "充值记录";
            $this->assign('rechargerecord', $res);
            $this->display();
        }

    public function morerecharge()
        {
            $pageSize = 10;
            $page = $_POST['page'] ? to_int(I("post.page")) : 1;
            $pageB = ($page - 1) * $pageSize;

            $agent_id = session('user_id');
            $where = array('agent_id' => $agent_id);
////        搜索
//            if (isset($_REQUEST['rechargeinfo'])) {
//                $rechargeinfo = trim(I('rechargeinfo'));
//                if ($rechargeinfo != '') {
//                    $cWhere['amount'] = array("eq", $rechargeinfo);
//                    $cWhere['_logic'] = 'or';
//                    $where['_complex'] = $cWhere;
//                }
//            }
            $res = M('agent_account')->where($where)->order('record_time DESC')->limit($pageB, $pageSize)->select();
            if(empty($res)){
                echo 1;
            }else{
                $this->assign('rechargerecord', $res);
                $this->display();
            }
        }
   //消费记录
    public function consumerecord() {
        $agent_id = session('user_id');
        $where = array('uid' => $agent_id,'is_pay'=>1);
//        搜索
        if (isset($_REQUEST['consumeinfo'])) {
            $consumeinfo = trim(I('consumeinfo'));
            if ($consumeinfo != '') {
                $cWhere['order_id'] = array("like", "%$consumeinfo%");
                $cWhere['uid'] = array("eq",$agent_id);
                $cWhere['is_pay']=array("eq",1);
//                $cWhere['price'] = array("eq", $consumeinfo);
                $cWhere['_logic'] = 'AND';
                $where =$cWhere;
            }
            $res = M("order")->where($where)->order('time DESC')->limit(0, 10)->select();
        }else
            ($res = M("order")->where($where)->order('time DESC')->limit(0, 10)->select());
        $this->site_title = "消费记录";
        $this->assign('consumerecord',$res);
        $this->display();
    }

    public function moreconsume(){
        $pageSize = 10;
        $page = $_POST['page'] ? to_int(I("post.page")) : 1;
        $pageB = ($page - 1) * $pageSize;
        $agent_id = session('user_id');
        $where = array('uid' => $agent_id);
//        搜索
        if (isset($_REQUEST['consumeinfo'])) {
            $consumeinfo = trim(I('consumeinfo'));
            if ($consumeinfo != '') {
                $cWhere['order_id'] = array("like", "%$consumeinfo%");
                $cWhere['uid'] = array("eq",$agent_id);
                $cWhere['_logic'] = 'AND';
                $where =$cWhere;
            }
            $res = M("order")->where($where)->order('time DESC')->limit($pageB, $pageSize)->select();
        }else
            ($res = M("order")->where($where)->order('time DESC')->limit($pageB, $pageSize)->select());
        if(empty($res)){
            echo 1;
        }else{
            $this->assign('consumerecord',$res);
            $this->display();
        }

    }

    public function normal_recharge()
    {
        $payments = M("payment")->where("is_open = 1 AND pay_code <> 'balance'")->field("p_id,p_name")->select();
        $this->assign('payments' ,$payments);
        $this->display();
    }

}